package com.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.ShoppingCarInfo;
import com.yunhe.inter.ShoppingCarInfoInter;
import com.yunhe.util.SqlSessionFactoryUtil;
/**
 * 避免点击添加购物车和在购物车中点击加减号的商品数量产生影响
 * @author Administrator
 *
 */

@WebServlet("/ShoppingCarInfoServlet_1")
public class ShoppingCarInfoServlet_1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public ShoppingCarInfoServlet_1() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取commodityID（点击加减号所对应的商品ID）
		Integer commodityID = Integer.parseInt(request.getParameter("commodityID")) ;
		Integer quantityPurchased = Integer.parseInt(request.getParameter("quantityPurchased"));
		
		//建立数据库连接
		SqlSession sqlSession =SqlSessionFactoryUtil.openSqlSession();
		ShoppingCarInfoInter mapper = sqlSession.getMapper(ShoppingCarInfoInter.class);
		/**
		 * 处理点击加减号的商品数量信息
		 */
			//更新对应ID的购买数量
			mapper.updateShoppingInfo(commodityID,quantityPurchased);
			
			
			
			
		//加载对应商品ID购物车信息供更新调用
			ShoppingCarInfo shoppingCar =  mapper.findShoppingInfo(commodityID);
			System.out.println(shoppingCar);
			JSONObject jsonCar = JSONObject.fromObject(shoppingCar);
		//查询购物车中的信息
		//将list集合放入request中
		PrintWriter out = response.getWriter();
		sqlSession.commit();
		sqlSession.close();
		out.write(jsonCar.toString());
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
